#import "KeyChainPlugin.h"
#import "UICKeyChainStore.h"

 
@implementation KeyChainPlugin
 
extern "C" {
        char* getKeyChainUser(const char* _key);
        void setKeyChainUser(const char* _key, const char* _value);
        void deleteKeyChainUser(const char* _key);
}
 
char* getKeyChainUser(const char* _key)
{
      NSLog(@"getKeyChainUser");
        NSString *userId = [UICKeyChainStore stringForKey:[NSString stringWithCString: _key encoding:NSUTF8StringEncoding]];
     
        if (userId == nil || [userId isEqualToString:@""]) {
                NSLog(@"No user information");
                userId = @"";
            }
     
        return makeStringCopy([userId UTF8String]);
}
 
void setKeyChainUser(const char* _key, const char* _value)
{
        [UICKeyChainStore setString:[NSString stringWithCString: _value encoding:NSUTF8StringEncoding] forKey:[NSString stringWithCString: _key encoding:NSUTF8StringEncoding]];
}
 
void deleteKeyChainUser(const char* _key)
{
        [UICKeyChainStore removeItemForKey:[NSString stringWithCString: _key encoding:NSUTF8StringEncoding]];
}
 
char* makeStringCopy(const char* str)
{
        if (str == NULL) {
                return NULL;
            }
     
        char* res = (char*)malloc(strlen(str) + 1);
        strcpy(res, str);
        return res;
}
 
@end

